<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Widthr;
use App\Models\Shop;

class WidthrShopController extends BaseController
{
    
    /**
     * 商户提现列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function widthrlist(Request $request) {

        $id = $this->shop_id;

        $status    = $request->input('status');

        $beginTime = $request->input('beginTime');//开始时间

        $endTime   = $request->input('endTime');//结束时间

        $withdr    = new Widthr();

        $status    != null && $withdr = $withdr->where('status', $status);

        $beginTime != null && $withdr = $withdr->where('created_at', '>=', $beginTime);

        $endTime   != null && $withdr = $withdr->where('created_at', '<=', $beginTime);

        $data      = $withdr->where('shopid', $id)->orderBy('created_at', 'desc')->paginate($this->pageSize);

        return self::json($data);

    }

    /**
     * 提现添加
     * @return [type] [description]
     */
    public function widthrAdd (Request $request) {

        $shop    = session('shopInfo');
        $money   = $request->input('money');

        //获得店铺的总金额
        $shopObj = Shop::find($shop['id']);
        //获得店铺总资金
        $money_all = $shopObj->help_money + $shopObj->money;

        if ( ($money - $money_all) > 0 ) {
            return self::error('金额不足');
        }   

        $Widthr = new Widthr();

        $Widthr->shopid    = $shop['id'];
        $Widthr->shopname  = $shop['shopname'];
        $Widthr->login     = $shop['login'];
        $Widthr->bank      = $shop['bank'];
        $Widthr->branch    = $shop['branch'];
        $Widthr->bank_card = $shop['bank_card'];
        $Widthr->status    = 1;
        $Widthr->money     = $money;

        $result  = $Widthr->save();

        return ($result == false) ? self::error('添加失败') : self::success('添加成功');
    }
}
